package main

import (
	"fmt"
	"net/http"
	"text/template"
)

var MyTemplate *template.Template

type Person struct {
	Name string
	Age  int
}

func initTemplate(fileName string) (err error) {
	MyTemplate, err = template.ParseFiles(fileName)
	if err != nil {
		fmt.Println("解析html出错", err)
		return
	}
	return
}

func Hello(respW http.ResponseWriter, req *http.Request) {
	fmt.Println("hello head ")
	p := Person{
		Name: "cici",
		Age:  20,
	}
	MyTemplate.Execute(respW, p)
}

func main() {
	/*	t, err := template.ParseFiles("D:\\Go workspace\\src\\web_template\\index.html")
		if err != nil {
			fmt.Println("解析html出错", err)
			return
		}
		p := Person{
			Name:"cici",
			age:"20",
		}
		err = t.Execute(os.Stdout, p)
		if err !=nil {
			fmt.Println("threr is an err ", err )
			return
		}*/
	initTemplate("D:\\Go workspace\\src\\web_template\\index.html")
	http.HandleFunc("/", Hello)
	err := http.ListenAndServe("localhost:8888", nil)
	if err != nil {
		fmt.Println("http listen failed", err)
	}
}
